<script setup>
import { ref } from 'vue'
import { listMemberCarService } from '@/api/car.js'

// 车辆列表
const carList = ref()

const listMemberCar = async () => {
  const res = await listMemberCarService()
  carList.value = res.data.data
}
listMemberCar()

// 车牌号格式化
const formatPlateNumber = (plateNumber) => {
  if (plateNumber.length < 3) return plateNumber
  const province = plateNumber.slice(0, 2)
  const rest = plateNumber.slice(2)
  return `${province}•${rest}`
}

// 获取车牌底色
const getPlateNumberBgc = (plateNumber) => {
  if (plateNumber.length === 8)
    return {
      'background-image': 'linear-gradient(to bottom, #F6F7F9 0%, #21C479 100%)'
    }
  if (/挂|学/.test(plateNumber)) return { 'background-color': '#f1bf00' }
  if (/警/.test(plateNumber)) return { 'background-color': '#fff' }
  if (/港|澳/.test(plateNumber)) return { 'background-color': '#000' }
  return { 'background-color': '#0041c5' }
}

// 获取车牌字体颜色
const getPlateNumberFontColor = (plateNumber) => {
  if (plateNumber.length === 8 || /挂|学|警/.test(plateNumber)) return '#000'
  return '#fff'
}

// 获取车牌边框颜色
const getPlateNumberBorderColor = (plateNumber) => {
  if (plateNumber.length === 8 || /挂|学|警/.test(plateNumber)) return '3px solid #000'
  return '3px solid #fff'
}

// 选择车辆
const emit = defineEmits(['selected'])
const handleSelect = (car) => {
  emit('selected', car)
}
</script>

<template>
  <div class="user-car">
    <div class="title">请选择车辆</div>
    <van-empty v-if="!carList?.length" image="search" description="无车辆信息" />
    <!-- 车辆列表 -->
    <van-swipe-cell v-for="car in carList" :key="car.id">
      <van-cell :border="false">
        <template #title>
          <div
            class="plate-number"
            :style="{
              ...getPlateNumberBgc(car.plateNumber),
              border: getPlateNumberBorderColor(car.plateNumber)
            }"
          >
            <van-password-input
              :value="formatPlateNumber(car.plateNumber)"
              :gutter="10"
              :mask="false"
              :length="formatPlateNumber(car.plateNumber).length"
              :style="{ '--plateNumber-color': getPlateNumberFontColor(car.plateNumber) }"
            />
          </div>
          <div class="car-info">
            <span class="brand">{{ car.brand }}</span>
            |
            <span class="model">{{ car.model }}</span>
          </div>
        </template>
      </van-cell>

      <template #right>
        <van-button
          square
          text="选择"
          type="primary"
          class="operate-button"
          @click="handleSelect(car)"
        />
      </template>
    </van-swipe-cell>
  </div>
</template>

<style lang="scss" scoped>
.user-car {
  background-color: #f7f8fa;

  .title {
    font-size: 16px;
    font-weight: 600;
    text-align: center;
    padding: 20px 0;
  }

  .van-cell {
    background-color: #fff;
    margin: 10px 0px;

    .plate-number {
      font-size: 20px;
      border-radius: 8px;
      padding: 10px 15px;
      margin-bottom: 8px;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.4);

      :deep(.van-password-input__security) {
        --van-password-input-height: 30px;

        .van-password-input__item {
          color: var(--plateNumber-color);
          background-color: rgba($color: #fff, $alpha: 0);
        }
      }
    }

    .car-info {
      color: #333;
      line-height: 1.4;
      font-size: 16px;
      display: flex;
      align-items: center;
      gap: 8px;
      padding-left: 10px;

      .brand {
        font-size: 18px;
        font-weight: 700;
        color: #000;
      }
      .model {
        line-height: 16px;
      }
    }
  }

  .operate-button {
    height: 100%;
  }
}
</style>
